$(function(){
  var player = polyvPlayer({
    wrap: '#player',
    width: 880,
    height: 500,
    vid: 'e785b2c81c9e018296671a1287e99615_e',
  });

  function changeVideo(url){
    setTimeout(function(){
      $('video').attr("src", url)
    }, 300)
  }

  $('#tree').treeview({
    collapseIcon: "glyphicon glyphicon-menu-down",//可收缩的节点图标
    expandIcon: "glyphicon glyphicon-menu-right",  //设置列表上中有子节点的图标
    selectedIcon: 'glyphicon glyphicon-play',
    levels: 3,
    preventUnselect: true,
    data: [{
      text: "第一章 经济学与经济体系的本质",
      selectable: false,
      nodes: [{
        text: "第一节 稀缺性",
        selectable: false,
        nodes: [{
          text: "稀缺性1",
          selectable: true,
          url: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
          state: {
            selected:true
          },
        },{
          selectable: true,
          text: "稀缺性2",
          url: "http://vjs.zencdn.net/v/oceans.mp4"
        }]
      },{
        text: "第二节 选择性",
        selectable: false,
        nodes: [{
          selectable: true,
          text: "选择",
          url: "https://media.w3.org/2010/05/sintel/trailer.mp4"
        }]
      },{
        text: "第三节 机会成本",
        selectable: false,
        nodes: [{
          selectable: true,
          text: "机会成本",
          url: "http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4"
        }]
      },{
        text: "第四节 收入循环流动",
        selectable: false,
        nodes: [{
          selectable: true,
          text: "收入循环流动",
          url: "http://dpv.videocc.net/e785b2c81c/5/e785b2c81c9e018296671a1287e99615_2.mp4"
        }]
      }]
    }],
    onNodeSelected: function (event, data) {
      var sels = $('#tree').treeview('getSelected');
      for (var i = 0; i < sels.length; i++) {
          if (sels[i].nodeId == data.nodeId) {
              continue;
          }
          $('#tree').treeview('unselectNode', [sels[i].nodeId, { silent: true }]);
      }
      $("#tree").treeview('selectNode', [data.nodeId, { silent: true }]);
      // 切换视频
      changeVideo(data.url)
    },
    onNodeUnselected: function (event, data) {
      $('#tree').treeview('selectNode', [data.nodeId, { silent: true }]);
      /* var sels = $('.tree').treeview('getSelected');
      for (var i = 0; i < sels.length; i++) {
        if (sels[i].nodeId == data.nodeId) {
            $(".tree").treeview('selectNode', [data.nodeId, { silent: true }]);
            continue;
        }
        $('.tree').treeview('selectNode', [sels[i].nodeId, { silent: true }]);
      } */
    }
  })
  
  //根据默认选中的菜单设置第一段默认视频
  function setDefalutVideo(){
    var sels = $('#tree').treeview('getSelected');
    changeVideo(sels[0].url)
  }
  setDefalutVideo()
})